package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class connection {
	public static final String driver = "org.gjt.mm.mysql.Driver";
	//定义MySQL数据库的驱动程序
	public static final String DBUtilss ="jdbc:mysql://192.168.43.154:3306/CarRentalTrade";
	//定义连接地址
	public static final String DBUac = "root";
	//mysql用户名
	public static final String DBPass = "123456";
	//mysql密码
	public static void main(String[] args) {
		Connection conn = null;//数据库连接
		try {
			Class.forName(driver);//加载驱动程序
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(DBUtilss, DBUac, DBPass);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 *	关闭数据库连接 
	 * 
	 */
		protected void closeAll(ResultSet rs, PreparedStatement ps,Connection conn) {
			if (rs!=null) {
				try {
					if(rs!=null)
						rs.close();
					if(ps!=null)
						ps.close();
					if(conn!=null)
						conn.close();
				} catch (SQLException e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		}
		/**
		 * 
		 *	查询方法
		 * 
		 */
		protected PreparedStatement prepareStatement(Connection conn,String sql,Object []ob){
			PreparedStatement ps=null;
			try {
				int index=1;
				ps = conn.prepareStatement(sql);
				if (ps != null && ob != null) {
					for (int i = 0; i < ob.length; i++) {
						ps.setObject(index, ob[i]);
						index++;
					}
				}
			} catch (SQLException e1) {
				// TODO: handle exception
				e1.printStackTrace();
			}
			return ps;
		}
	
}






